﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for CommentPhoto
    /// </summary>
    public class CommentPhoto
    {

        private int _CommentID;
        private int _PhotoID;
        private int _FriendID;
        private String _Content;
        private DateTime _CreateDate;

        public int CommentID
        {
            get
            {
                return _CommentID;
            }
            set
            {
                _CommentID = value;
            }
        }

        public int PhotoID
        {
            get
            {
                return _PhotoID;
            }
            set
            {
                _PhotoID = value;
            }
        }

        public int FriendID
        {
            get
            {
                return _FriendID;
            }
            set
            {
                _FriendID = value;
            }
        }

        public String Content
        {
            get
            {
                return _Content;
            }
            set
            {
                _Content = value;
            }
        }

        public DateTime CreateDate
        {
            get
            {
                return _CreateDate;
            }
            set
            {
                _CreateDate = value;
            }
        }

        public CommentPhoto()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public CommentPhoto(SqlDataReader SqlReader)
        {
            if (SqlReader != null && !SqlReader.IsClosed)
            {
                _CommentID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    _PhotoID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    _FriendID = SqlReader.GetInt32(2);
                }
                if (!SqlReader.IsDBNull(3))
                {
                    _Content = SqlReader.GetString(3);
                }
                if (!SqlReader.IsDBNull(4))
                {
                    _CreateDate = SqlReader.GetDateTime(4);
                }
            }
        }

        public static DataTable GetAll()
        {
            DBHelp Dbh = new DBHelp();
            DataTable dt = new DataTable();
            String cmd = "CommentPhoto_GetAll";
            dt = Dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static CommentPhoto GetInfo(int CommentID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@comment_id", CommentID);
            CommentPhoto Comment = new CommentPhoto();
            string cmd = "CommentAlbum_GetInfo";
            SqlDataReader SqlReader = Dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (SqlReader.Read())
            {
                Comment.CommentID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    Comment.PhotoID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    Comment.FriendID = SqlReader.GetInt32(2);
                }
                if (!SqlReader.IsDBNull(3))
                {
                    Comment.Content = SqlReader.GetString(3);
                }
                if (!SqlReader.IsDBNull(4))
                {
                    Comment.CreateDate = SqlReader.GetDateTime(4);
                }
            }
            return Comment;
        }


        public static int Insert(CommentPhoto Comment)
        {
            DBHelp Dbh = new DBHelp();
            SqlParameter ComementID = new SqlParameter();
            ComementID.DbType = DbType.Int32;
            ComementID.Direction = ParameterDirection.Output;
            ComementID.ParameterName = @"@comment_id";
            Dbh.AddParameter(ComementID);
            Dbh.AddParameter("@photo_id", Comment.PhotoID);
            Dbh.AddParameter("@friend_id", Comment.FriendID);
            Dbh.AddParameter("@text", Comment.Content);
            Dbh.AddParameter("@created_date", Comment.CreateDate);

            string cmd = "CommentPhoto_Insert";
            try
            {
                Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
                return (int)ComementID.Value;
            }
            catch (System.Exception)
            {
                return -1;
            }

        }

        public static void Update(CommentPhoto Comment)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@comment_id", Comment.CommentID);
            Dbh.AddParameter("@photo_id", Comment.PhotoID);
            Dbh.AddParameter("@friend_id", Comment.FriendID);
            Dbh.AddParameter("@text", Comment.Content);
            Dbh.AddParameter("@created_date", Comment.CreateDate);

            string cmd = "CommentPhoto_Update";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        public static void Delete(int CommentID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@comment_id", CommentID);
            string cmd = "CommentPhoto_Delete";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    }
}
